import { Body, Controller, Get, Post, Res, UseInterceptors, Request, HttpException, HttpStatus } from '@nestjs/common';
import { ValidRSAInterceptor } from 'src/interceptor/validRSA';
import { ValidTokenInterceptor } from 'src/interceptor/validToken';
import { apibefore } from 'src/router';
import { WatchRecord } from './watchRecord.entity';
import { WatchRecordService } from './watchRecord.service';

@Controller(apibefore('/watchRecord'))
@UseInterceptors(ValidTokenInterceptor)
@UseInterceptors(ValidRSAInterceptor)
export class WatchRecordController {
  constructor(private readonly service: WatchRecordService) {}

  @Post('/create')
  async create(@Body() data: any, @Request() req) {
    const wx_user = req.headers.wx_user
    let saveData = new WatchRecord();
    saveData.video_id = data.video_id
    saveData.wx_user = wx_user
    if(await this.service.findByVideoIdAndWxUser(data.video_id, wx_user)){
      return true
    }
    saveData.create_time = new Date()
    try{
      return await this.service.create(saveData, data.work_type);
    }catch(err) {
      return false
    }
  }
  @Get('/findAll')
  async findAll(){
    return await this.service.findAll()
  }
}
